package cn.kgc.itrip.controller;

import cn.kgc.itrip.common.DtoUtil;
import cn.kgc.itrip.dto.Dto;
import cn.kgc.itrip.service.hotelOrderService.HotelOrderService;
import cn.kgc.itrip.vo.order.ItripAddHotelOrderVO;
import cn.kgc.itrip.vo.order.ItripModifyHotelOrderVO;
import cn.kgc.itrip.vo.order.ItripSearchOrderVO;
import cn.kgc.itrip.vo.order.ValidateRoomStoreVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @program: love_travel
 * @description: 酒店订单
 * @author: Mr.Wang
 * @create: 2020-04-02 15:19
 **/

@RestController
@RequestMapping("/api/hotelorder")
public class HotelOrderController {

    @Resource
    private HotelOrderService hotelOrderService;

    /**
     * 生成订单前,获取预订信息
     * @param validateRoomStoreVO
     * @return
     */
    @PostMapping(value = "/getpreorderinfo",produces = "application/json")
    public Dto getPreOrderInfo(@RequestBody ValidateRoomStoreVO validateRoomStoreVO, HttpServletRequest request){
        Dto dto=null;
        String token = request.getHeader("token");
        String userAgent = request.getHeader("user-agent");
        try {
            dto=hotelOrderService.addNewOrder(validateRoomStoreVO,token,userAgent);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常","100513");
        }
        return dto;
    }


    /**
     * 生成订单
     * @param itripAddHotelOrderVO
     * @param request
     * @return
     */
    @PostMapping(value = "/addhotelorder",produces = "application/json")
    public Dto addHotelOrder(@RequestBody ItripAddHotelOrderVO itripAddHotelOrderVO,HttpServletRequest request){
        Dto dto=null;
        String token = request.getHeader("token");
        String userAgent = request.getHeader("user-agent");
        try {
            dto=hotelOrderService.addHotelOrder(itripAddHotelOrderVO,token,userAgent);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("生成订单失败","100505");
        }
        return dto;
    }

    /**
     * 修改订房日期验证是否有房
     * @param validateRoomStoreVO
     * @param request
     * @return
     */
    @PostMapping(value = "/validateroomstore",produces = "application/json")
    public Dto valiDateRoomStore(@RequestBody ValidateRoomStoreVO validateRoomStoreVO,HttpServletRequest request){
        Dto dto=null;
        String token = request.getHeader("token");
        String userAgent = request.getHeader("user-agent");
        try {
            dto=hotelOrderService.valiDateRoomStore(validateRoomStoreVO,token,userAgent);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("生成订单失败","100505");
        }
        return dto;
    }

    /**
     * 根据订单ID查看个人订单详情
     * @param orderId
     * @return
     */
    @GetMapping(value = "/getpersonalorderinfo/{orderId}",produces = "application/json")
    public Dto getPersonalOrderInfo(@PathVariable String orderId,HttpServletRequest request){
        Dto dto=null;
        String token = request.getHeader("token");
        String userAgent = request.getHeader("user-agent");
        try {
            dto=hotelOrderService.findPersonalOrderInfo(orderId,token,userAgent);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取个人订单信息错误","100527");
        }
        return dto;
    }

    /**
     * 根据订单ID查看个人订单详情-房型相关信息
     * @param orderId
     * @param request
     * @return
     */
    @GetMapping(value = "/getpersonalorderroominfo/{orderId}",produces = "application/json")
    public Dto getpersonalorderroominfo(@PathVariable String orderId,HttpServletRequest request ){
        Dto dto=null;
        String token = request.getHeader("token");
        String userAgent = request.getHeader("user-agent");
        try {
            dto=hotelOrderService.findPersonalOrderRoomInfo(orderId,token,userAgent);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取个人订单房型信息错误","100531");
        }
        return dto;
    }

    /**
     * 根据个人订单列表，并分页显示
     * @param itripSearchOrderVO
     * @param request
     * @return
     */
    @PostMapping(value = "/getpersonalorderlist",produces = "application/json")
    public Dto getPersonalOrderList(@RequestBody ItripSearchOrderVO itripSearchOrderVO,HttpServletRequest request){
        Dto dto=null;
        String token = request.getHeader("token");
        String userAgent = request.getHeader("user-agent");
        try {
            dto=hotelOrderService.findPersonalOrderList(itripSearchOrderVO,token,userAgent);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取个人订单房型信息错误","100531");
        }
        return dto;
    }

    /**
     * 根据订单ID获取订单信息
     * @param orderId
     * @return
     */
    @GetMapping(value = "/queryOrderById/{orderId}",produces = "application/json")
    public Dto queryOrderById(@PathVariable Integer orderId){
        Dto dto=null;
        try {
            dto=hotelOrderService.findOrderById(orderId);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常","100534");
        }
        return dto;
    }

    /**
     * 修改订单的支付方式和状态
     * @param itripModifyHotelOrderVO
     * @param request
     * @return
     */
    @PostMapping(value = "/updateorderstatusandpaytype",produces = "application/json")
    public Dto updateOrderStatusAndPayType(@RequestBody ItripModifyHotelOrderVO itripModifyHotelOrderVO,HttpServletRequest request){
        Dto dto=null;
        String token = request.getHeader("token");
        String userAgent = request.getHeader("user-agent");
        try {
            dto=hotelOrderService.midOrderStatusAndPayType(itripModifyHotelOrderVO,token,userAgent);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常","100534");
        }
        return dto;
    }

    /**
     * 支付成功后查询订单信息
     * @param id
     * @param request
     * @return
     */
    @PostMapping(value = "/querysuccessorderinfo/{id}",produces = "application/json")
    public Dto querySuccessOrderInfo(@PathVariable Integer id,HttpServletRequest request){
        Dto dto=null;
        String token = request.getHeader("token");
        String userAgent = request.getHeader("user-agent");
        try {
            dto=hotelOrderService.findSuccessOrderInfo(id,token,userAgent);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常","100534");
        }
        return dto;
    }
}
